package com.hnust.controller;

import com.hnust.domain.CourseGroup;
import com.hnust.domain.record.*;
import com.hnust.domain.request.UpdateCoursePointReqBody;
import com.hnust.domain.request.UpdateInfoReqBody;
import com.hnust.service.InfoManageService;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hnust.service.TokenService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 个人信息管理板块接口
 * @author 焦硕洁
 * @date 2020/10/29
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/teacher")
public class InfoManageController {

    @Autowired
    private InfoManageService infoManageService;
    @Autowired
    private  TokenService tokenService;

    @GetMapping("/getInfo")
    public Result<UserInfo> getInfo(
            @RequestParam(value = "teacherId", required = false) String id,
            @RequestParam(value = "token", required = false) String token) {
        UserInfo test = infoManageService.getInfo(id, token);
        if(test==null){
            return new Result<>(0, "获取个人信息信息失败", null);
  }
else {
            return new Result<>(1, "获取个人信息成功", test);
        }
    }

    @GetMapping("/updateCollege")
    public Result<String> updateCollege(
            @RequestParam(value = "teacherId", required = false) String id,
            @RequestParam(value = "token", required = false) String token,
            @RequestParam(value = "collegeId", required = false) String collegeId) {
        String msg = infoManageService.updateCollege(id,token,collegeId);
        if ("修改学院成功".equals(msg)) {
            return new Result<>(1, "修改学院成功", null);
        } else {
            return new Result<>(0, msg, null);
        }
    }

    @PostMapping("/updateInfo")
    public Result<String> updateInfo(@RequestBody UpdateInfoReqBody reqBody){
        String msg=infoManageService.updateInfo(reqBody.getUpdateUserInfo(),reqBody.getToken(),reqBody.getId());
        if("修改信息成功".equals(msg)){
            return new Result<>(1,"修改信息成功",null);
        }else{
            return new Result<>(0,msg,null);
        }

    }
    @GetMapping("/getCourseGroup")
    public Result<List<GroupInfo>> getCourseGroup(
            @RequestParam(value = "collegeId", required = false) String collegeId,
            @RequestParam(value = "token", required = false) String token) {
        List<GroupInfo> groupInfo = infoManageService.getCourseGroup(collegeId, token);
        if(groupInfo==null){
            return new Result<>(0, "获取课程组信息失败", null);
        }
     else{
            return new Result<>(1, "获取课程组信息成功", groupInfo);
        }

    }
    //获取老师正在等待审核的课程组         （彭鑫淼）
    @GetMapping("/getWaitCourseGroup")
    public Result<List<GroupInfo>> getWaitCourseGroup(
            @RequestParam(value = "teacherId", required = false) String teacherId,
            @RequestParam(value = "token", required = false) String token) {
        List<GroupInfo> groupInfo = infoManageService.getWaitCourseGroup(teacherId, token);
        if(groupInfo==null){
            return new Result<>(0, "获取课程组信息失败", null);
        }
        else{
            return new Result<>(1, "获取课程组信息成功", groupInfo);
        }

    }
    @PostMapping("/applyCourse")
    public Result<String> applyCourse( @RequestParam("teacherId") String id,
                                       @RequestParam("groupId") String groupId,
                                       @RequestParam("token") String token){
        String msg=infoManageService.applyCourse(id,groupId,token);
        if("申请加入课程组成功".equals(msg)){
            return new Result<>(1,"申请加入课程组成功",null);
        }else{
            return new Result<>(0,msg,null);
        }

    }

    @PostMapping("/outCourse")
    public Result<String> outCourse( @RequestParam("teacherId") String id,
                                       @RequestParam("courseGroupId") String groupId,
                                       @RequestParam("token") String token){
        String msg=infoManageService.outCourse(id,groupId,token);
        if("退出课程组成功".equals(msg)){
            return new Result<>(1,"退出课程组成功",null);
        }else{
            return new Result<>(0,msg,null);
        }

    }

    @GetMapping("/getCourseInfo")
    public Result<GroupResult> getCourseInfo(@RequestParam(value = "teacherId", required = false)String id,
                                             @RequestParam(value = "groupId" ,required = false)String groupId,
                                             @RequestParam(value = "token", required = false)String token,
                                             @RequestParam(value = "nowPage", required = false) Integer nowPage){
        try {
            if(infoManageService.getCourseInfo(id,groupId,token,nowPage)!=null){
                return new Result<>(1,"获取课程组信息成功",infoManageService.getCourseInfo(id,groupId,token,nowPage));
            }else{
                return new Result<>(0,"获取课程组信息失败",null);
            }

        }catch (Exception e){
            return new Result<>(0,"获取课程组信息失败",null);
        }
    }
    @GetMapping("/getCourseTeaByName")
    public Result<List<GroupMember>> getCourseTeaByName(
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "token", required = false) String token,
            @RequestParam(value = "groupId", required = false) String groupId) {

            if(infoManageService.getCourseTeaByName(teacherName,token,groupId)!=null){
                return new Result<>(1,"查询成功",infoManageService.getCourseTeaByName(teacherName,token,groupId));
            }else{
                return new Result<>(0,"查询失败",null);
            }
    }

    @PostMapping("/updateCoursePoint")
    public Result<String> updateCoursePoint(@RequestBody UpdateCoursePointReqBody reqBody){
        String msg=infoManageService.updateCoursePoint(reqBody.getCoursePoint(),reqBody.getToken(),reqBody.getId());

        if("修改毕业要求指标点成功".equals(msg)){
            return new Result<>(1,msg,null);
        }else{
            return new Result<>(0,msg,null);
        }

    }



    @GetMapping("/getCoursePoint")
    public Result<List<CoursePoint>> getCoursePoint(
            @RequestParam(value = "majorId", required = false) String majorId,
            @RequestParam(value = "token", required = false) String token,
            @RequestParam(value = "teacherId", required = false) String id) {
        if (infoManageService.getCoursePoint(majorId,token,id)!=null) {
            return new Result<>(1, "获取毕业指标点成功",infoManageService.getCoursePoint(majorId,token,id) );
        } else {
            return new Result<>(0, "获取毕业指标点失败", null);
        }
    }

}


